(function(){
	angular.module('app')
		.directive('thirdIframe', ['$timeout','$compile', '$document','$rootScope', 'myUtils', function( $timeout,$compile,$document,$rootScope, myUtils ){
			return {
				restrict: 'A',
		      	scope: {
			        ngIframe: '=thirdIframe',
			        ngLoading : '=loading',
			        emitData : '&emitData'
				},
				link: function (scope,element, attrs) {
			     	scope.ngLoading = true;
			     	element.addClass('hidden');
			     	scope.$watch('ngIframe', function( data ){
			     		if( data ){
			     			scope.url = data;
			     			$timeout(function(){
			     				element.attr('src', data);
			     			}, 0);
			     			$timeout(function(){
			     				scope.ngLoading = false;
			     			}, 1000);
			     			element.load(function(){
			     				element.removeClass('hidden')
			     			})
			     		}
			     	});
			     	scope.refresh = function( item ){
			     		if( item.url == 'app.extend.third'){
			     			$timeout(function(){
			     				element.attr('src', scope.url);
			     			}, 0);
			     		}
			     	};
	     			window.emitParent = function( data ){
	     				if( data.param ){
	     					var breadCrumbs = $(document.body).find('.navbar').find('.bread-crumbs');
	     					breadCrumbs.find('.third-item').remove();
	     					$rootScope.thirdInfo = data;
	     					scope.crumb = [];
	     					var titleList = data.param.label.split('/');
					 		scope.crumb[0] = {title : titleList[0]+' / ',url:'app.extend'};
					 		scope.crumb[1] = {title : titleList[1]+' / ',url:'app.extend.third'};
					 		scope.crumb[2] = {title : titleList[2],url:'app.extend.third'};
					 		if( !breadCrumbs.find('.third-item').length ){
					 			breadCrumbs.append(
					 				'<div class="third-item">'
									+'<span ng-repeat="item in crumb">'
										+'<a ng-click="refresh(item)" ui-sref="{{item.url}}">{{item.title}}</a>'
									+'<span>'
									+'</div>');
						 		$compile( breadCrumbs )(scope);
					 		}
					 		scope.$apply(function(){
						 		scope.crumb = scope.crumb;
						 	})
	     				}
                         if( data.height ){
     						element.height('100%');
 						}
	     				
	     			};
				}
			}
		}]);
})();
